package com.athena.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.athena.common.core.domain.model.LoginUser;
import com.athena.common.helper.LoginHelper;
import com.athena.common.utils.DateUtils;
import com.athena.common.utils.StringUtils;
import com.athena.common.core.page.TableDataInfo;
import com.athena.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.athena.crm.constant.CommonConstant;
import com.athena.crm.domain.CrmClue;
import com.athena.crm.domain.bo.CrmClueFollrecordsAddBo;
import com.athena.crm.mapper.CrmClueFollrecordsMapper;
import com.athena.crm.mapper.CrmClueMapper;
import com.athena.crm.service.ICrmClueService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.athena.crm.domain.bo.CrmClueFollrecordsBo;
import com.athena.crm.domain.vo.CrmClueFollrecordsVo;
import com.athena.crm.domain.CrmClueFollrecords;
import com.athena.crm.service.ICrmClueFollrecordsService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户跟进记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-07
 */
@RequiredArgsConstructor
@Service
public class CrmClueFollrecordsServiceImpl implements ICrmClueFollrecordsService {

    private final CrmClueFollrecordsMapper baseMapper;

    private final CrmClueMapper crmClueMapper;

    /**
     * 查询客户跟进记录
     */
    @Override
    public CrmClueFollrecordsVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户跟进记录列表
     */
    @Override
    public TableDataInfo<CrmClueFollrecordsVo> queryPageList(CrmClueFollrecordsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CrmClueFollrecords> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(CrmClueFollrecords::getUpdateTime);
        Page<CrmClueFollrecordsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户跟进记录列表
     */
    @Override
    public List<CrmClueFollrecordsVo> queryList(CrmClueFollrecordsBo bo) {
        LambdaQueryWrapper<CrmClueFollrecords> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(CrmClueFollrecords::getUpdateTime);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CrmClueFollrecords> buildQueryWrapper(CrmClueFollrecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CrmClueFollrecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getClueId()), CrmClueFollrecords::getClueId, bo.getClueId());
        lqw.eq(StringUtils.isNotBlank(bo.getFlowType()), CrmClueFollrecords::getFlowType, bo.getFlowType());
        lqw.eq(StringUtils.isNotBlank(bo.getFollowUser()), CrmClueFollrecords::getFollowUser, bo.getFollowUser());
        lqw.eq(StringUtils.isNotBlank(bo.getFollowContent()), CrmClueFollrecords::getFollowContent, bo.getFollowContent());
        lqw.eq(bo.getIsDeleted() != null, CrmClueFollrecords::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增客户跟进记录
     */
    @Override
    public Boolean insertByBo(CrmClueFollrecordsBo bo) {
        CrmClueFollrecords add = BeanUtil.toBean(bo, CrmClueFollrecords.class);
        validEntityBeforeSave(add);
        add.setFlowType(CommonConstant.FOLLOW_TYPE_SALES_FLOW);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户跟进记录
     */
    @Override
    public Boolean updateByBo(CrmClueFollrecordsBo bo) {
        CrmClueFollrecords update = BeanUtil.toBean(bo, CrmClueFollrecords.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CrmClueFollrecords entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户跟进记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean insertByBbo(CrmClueFollrecordsAddBo bbo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        CrmClueFollrecordsBo bo = new CrmClueFollrecordsBo();
        bo.setClueId(bbo.getClueId());
        bo.setFollowContent(bbo.getFollowContent());
        bo.setFlowType(bbo.getFlowType());
        CrmClueFollrecords add = BeanUtil.toBean(bo, CrmClueFollrecords.class);
        validEntityBeforeSave(add);
        add.setFollowUser(loginUser.getUsername());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            Date nextFollowTime = DateUtils.parseDate(bbo.getNextFollowTime());
            CrmClue crmClue = crmClueMapper.selectById(bbo.getClueId());
            if (crmClue != null) {
                crmClue.setFollowStatus(bbo.getFollowStatus());
                crmClue.setLatestFollowRecords(bbo.getFollowContent());
                crmClue.setRealityFollowRecords(new Date());
                crmClue.setNextFollowTime(nextFollowTime);
            }
            crmClueMapper.updateById(crmClue);
        }
        return flag;
    }

    @Override
    public List<CrmClueFollrecords> batchInsertByBo(List<CrmClueFollrecords> customerFollrecords) {
        boolean flag = baseMapper.insertBatch(customerFollrecords);
        return customerFollrecords;
    }
}
